<?php
declare (strict_types=1);

namespace app\command;

use app\api\logic\LogicalProcess;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;


class GameDfj extends Command
{
    protected function configure()
    {
        $this->setName('app\command\gameDfj');

    }

    //大反击结算逻辑
    protected function execute(Input $input, Output $output)
    {
        $dfjRecord = Db::name("dfj_record")->order("create_time desc")->where("is_over", false)->find();
        if (!empty($dfjRecord)) {
            //返还所有金箍棒
            $userDfj = Db::name("user_dfj_record")->where("dfj_id", $dfjRecord['id'])->column("jgb_id", "user_id");
            if (!empty($userDfj)) {
                Db::name("dfj_record")->order("create_time desc")->where("is_over", false)->update([
                    "is_over" => true,
                    "date" => datetime(time(), "Y-m-d")
                ]);
                Db::name("user_jgb")->whereIn("id", $userDfj)->update(['is_use' => false]);
                //结算瓜分
                $count = count($userDfj);
                $divide = bcdiv($dfjRecord['total_this'], (string)$count, 6);
                foreach ($userDfj as $k => $v) {
                    if ($divide > 0) {
                        (new LogicalProcess())->wallletChange($k, $divide, 6, 1, 1, '大反击瓜分');
                    }
                }
            }
            $gameConfig = Db::name("game_config")->where("id", 1)->find();
            //大反击瓜分比例
            $dfjCarveUpScale = $gameConfig['dfj_carve_up_scale'] / 100;
            Db::name("dfj_record")->insert([
                "create_time" => datetime(time()),
                'is_over' => false,
                'total_this' => bcmul($gameConfig['dfj_prize_pool'], (string)$dfjCarveUpScale, 2)
            ]);
            Db::name("game_config")->where("id", 1)->update([
                "dfj_prize_pool" => $gameConfig['dfj_prize_pool'] - bcmul($gameConfig['dfj_prize_pool'], (string)$dfjCarveUpScale, 2)
            ]);
        }
        $output->writeln('success');
        exit;
    }
}